<?php

namespace app\wxauth\controller;

use Alipay\Alipayobj;
use app\api\controller\Cards;
use app\api\controller\CardsThree;
use app\api\controller\CreateNotify;
use app\api\controller\MachThree;
use app\api\controller\OfficialPay;
use app\api\controller\Pay;
use app\api\controller\Pub;
use app\api\job\SendNotify;
use app\common\controller\Bonus;
use app\common\controller\ChildCardModify;
use app\common\controller\Deviceobj;
use app\common\controller\TemplateMsg;
use app\common\model\Agent;
use app\common\model\CardCzgive;
use app\common\model\CardGeneral;
use app\common\model\CardMember;
use app\common\model\CardRank;
use app\common\model\CardUserGet;
use app\common\model\Merchant;
use app\common\model\Paylog;
use app\common\model\System;
use app\common\model\WxOpen;
use Endroid\QrCode\QrCode;
use think\Config;
use think\Controller;
use think\Db;
use think\helper\hash\Md5;
use Wechat\Wxobjopen;
use Weixinpay\Wxtk;

class Index extends Controller
{

    public function getauthopenid(){
        $param = request()->param();
        var_dump($param);
        //获取授权为空时向微信发送请求
        if (!empty($param['mid'])) {
            $option = $this->options($param['mid']);

            $WxObj = new Wxobjopen($option);
            //获取授权openid
            $userinfo = $WxObj->getOpenoauthAccessToken();

            //获取授权为空时向微信发送请求
            if (empty($userinfo)) {
                $callback = request()->url(true);
                var_dump($userinfo);
                $url = $WxObj->getopenOauthCode($callback, '', 'snsapi_base');
                var_dump($url);
                exit();
                header("Location:$url");
                exit();
            }else{
                var_dump($userinfo);
                $urls = base64_decode($param['url']);
                header("Location:$urls");
            }
        }
    }




    public function options($mid)
    {
        $Merchant = new Merchant();
        $Agent = new Agent();
        $WxOpen = new WxOpen();
        $map['id'] = $mid;
        $merchantinfo = $Merchant->where($map)->field('wx_open_id,agent')->find();
        $mab['id'] = $merchantinfo['wx_open_id'];
        $users = $WxOpen->where($mab)->find();
        $config = $Agent->where(array('id' => $merchantinfo['agent']))->find();
        $options = array(
            'component_token' => $config['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $config['open_ticket'],
            'authorizer_appid' => $users['authorizer_appid'],
            'authorizer_refresh_token' => $users['authorizer_refresh_token'],
        );
        return $options;
    }
}
